package com.lynn.salary.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.lynn.salary.entity.EmpRole;
import com.lynn.salary.entity.Employee;
import com.lynn.salary.entity.Permission;
import com.lynn.salary.entity.Role;
import com.lynn.salary.entity.SystemArgument;
import com.lynn.salary.pojo.TreeNode;
import com.lynn.salary.service.IEmployeeService;
import com.lynn.salary.util.ToolMethod;

public class EmployeeService extends BaseService implements IEmployeeService {

	public Integer totalCount() throws Exception {
		String tn = ToolMethod.getTableName(Employee.class);
		String sql = "select count(*) tc from " + tn;
		List<Map<String, Object>> result = dao.executeSQL(sql);

		if (result != null && result.size() != 0) {
			Map<String, Object> map = result.get(0);
			Object o = map.get("tc");
			Integer i = Integer.parseInt(o.toString());
			return i;
		}
		return null;
	}

	public Employee findById(Serializable id) throws Exception {
		System.out.println("EmployeeService::findById  " + id);
		return null;
	}

	public List<Map<String, Object>> findEmployeeByLimit(Integer currentPage,
			Integer numPerPage) throws Exception {
		String tn = ToolMethod.getTableName(Permission.class);

		String sql = "select * from " + tn + " limit " + (currentPage - 1)
				* numPerPage + "," + numPerPage;

		return dao.executeSQL(sql);
	}

	public void save(Employee emp) throws Exception {
		this.dao.save(emp);
	}

	public void delete(String deleteId) throws Exception {
		this.dao.deleteById(Employee.class, deleteId);
	}

	public Employee findEmployeeById(String editId) throws Exception {
		Employee emp = dao.getById(Employee.class, editId);
		return emp;
	}

	public void updateEmployee(Employee emp) throws Exception {
		dao.update(emp);
	}

	public Employee checkLoginUser(String username, String password)
			throws Exception {
		Employee e = dao.getById(Employee.class, username);
		if (e != null && e.getPassword().equals(password))
			return e;
		return null;
	}

	public List<Role> getRoles(String eid) throws Exception {

		List<Map<String, Object>> rs = dao.getPartList(EmpRole.class,
				new String[] { "$roleId#id id", "$roleId#name name" },
				"empId='" + eid + "'");
		List<Role> roles = new ArrayList<Role>();

		for (Map<String, Object> r : rs) {
			Role tmp = new Role();
			tmp.setId((String) r.get("id"));
			tmp.setName((String) r.get("name"));
			roles.add(tmp);
		}

		return roles;
	}

	public List<SystemArgument> findByType(String selectType) throws Exception {
		List<SystemArgument> r = dao.getList(SystemArgument.class, "type='"
				+ selectType + "'");
		return r;

	}

	/*
	 * public List<SalaryKind> findAllKinds()throws Exception{ List<SalaryKind>
	 * sk= dao.getList(SalaryKind.class, ""); return sk; }
	 */

	public Employee findManager(String depId) throws Exception {
		// 部门经理查找

		SystemArgument sa = dao.getById(SystemArgument.class, depId);
		String roleName = "部门经理";

		List<Map<String, Object>> rs = dao.getPartList(EmpRole.class,
				new String[] { "empId", "$empId#name", "$roleId#name name" },
				"$$a3.name='" + roleName + "' and $$a2.departmentId='" + depId
						+ "'");
		String empId = (String) rs.get(0).get("empId");
		Employee e2 = dao.getById(Employee.class, empId);
		return e2;
	}

	public List<TreeNode> findAll() throws Exception {
		/*
		 * ISaService sas = (ISaService) ObjectFactory.getTarget("saService");
		 * List<SystemArgument> deps = sas.findByType("部门"); List<TreeNode>
		 * results = new ArrayList<TreeNode>(); for (Iterator iterator =
		 * deps.iterator(); iterator.hasNext();) { SystemArgument systemArgument
		 * = (SystemArgument) iterator.next(); TreeNode dep = new TreeNode();
		 * dep.setName(systemArgument.getName()); dep.setData(systemArgument);
		 * dep.setFlag("dep"); List<TreeNode> childs = new
		 * ArrayList<TreeNode>(); List<Employee> emps =
		 * dao.getList(Employee.class, "departmentId='" +
		 * systemArgument.getCode() + "'"); for (Iterator iterator2 =
		 * emps.iterator(); iterator2.hasNext();) { Employee employee =
		 * (Employee) iterator2.next(); TreeNode e = new TreeNode();
		 * e.setData(employee); e.setName(employee.getName()); e.setParent(dep);
		 * e.setFlag("emp"); childs.add(e); } dep.setChilds(childs);
		 * results.add(dep); }
		 */
		return null;
	}

	public String havePublishSalary(String month) throws Exception {
		List<Map<String, Object>> data = dao
				.executeSQL("select count(id) cou from empSalaryItem where smonth like '"
						+ month + "'");
		String a = "" + data.get(0).get("cou");

		return a;
	}

	public List<Role> findEmpNotContainRole(String empid) throws Exception {
		/*
		 * String sql = "select roleid from emprole where empid='" + empid +
		 * "'"; return dao.getList(Role.class, "id not in (" + sql + ")");
		 */
		return null;
	}

	public void updateEmpRoles(List<String> newRoles, String empId)
			throws Exception {
		
	}
}